package com.suntront.module_metertask.meterdetail

import androidx.lifecycle.rxLifeScope
import com.mtjsoft.www.kotlinmvputils.base.BaseRecycleViewModel
import com.mtjsoft.www.kotlinmvputils.imp.AndCallBackListImp
import com.mtjsoft.www.kotlinmvputils.model.LoadState
import com.mtjsoft.www.kotlinmvputils.rxhttp.error.code
import com.mtjsoft.www.kotlinmvputils.rxhttp.error.msg
import com.suntront.lib_db.AppDatabase
import com.suntront.lib_db.table.UserT
import kotlinx.coroutines.async

/**
 * @author mtj
 * @Package com.suntront.module_metertask.meterdetail
 * @date 2020-06-01 08:59:56
 */

class MeterDetailViewModel : BaseRecycleViewModel<UserT>() {

    var rosterId = 0
    /**
     * 获取列表数据
     */
    override fun getListDataInThread(pager: Int,callBackListImp: AndCallBackListImp<UserT>) {
        rxLifeScope.launch({
            // 异步读取本地数据库
            val list: MutableList<UserT> = async {
                AppDatabase.getDb().userDao().queryUpLoadByRosterId(rosterId)
            }.await()
            callBackListImp.onSuccess(list)
        }, {
            //异常回调，这里的it为Throwable类型
            val code = it.code
            val msg = it.msg
            showToastUI(msg)
            loadState.postValue(LoadState.FAILED)
        })
    }
}